<?php
require_once('functions.php');

function getItems()
{
        $conn = getConnection();
        return $conn->query('SELECT * FROM Item');
}

function getItem($sn)
{
        $conn = getConnection();
        $results = $conn->query("SELECT * FROM Item WHERE serialNumber = '$sn'"); 
        $row = $results->fetch_assoc();
        $conn->close();
        return $row;
}

function getDistintSerialnumberAndName()
{
		$conn = getConnection();
        return $conn->query('SELECT DISTINCT serialNumber, name FROM Item');
}

function deleteItem($serialNumber)
{
		$conn = getConnection();
		$conn->query("DELETE FROM Item WHERE serialNumber = '$serialNumber'");
		$conn->close();
		header('Location: index.php');
}

function saveItem(&$row)

{
        $conn = getConnection();
        $row2 = escapeRow($row, $conn);
        
        if(!($row2['add'] == "add"))
        	$sql = "UPDATE Item SET serialNumber='$row2[serialNumber]', name='$row2[name]', price='$row2[price]', quantity='$row2[quantity]', manufacturer='$row2[manufacturer]' WHERE serialNumber = '$row2[serialNumber]'";
        else
        	$sql = "INSERT INTO Item VALUES ('$row2[serialNumber]','$row2[name]', '$row2[price]', '$row2[quantity]', '$row2[manufacturer]', NOW(),NULL)";
        
		echo $sql;	
        $results = $conn->query($sql);
        $error = $conn->error;
        
        $conn->close();
       
        return $error == '' ? true : array('SQL Error' => $error);
}

function blankItem()
{
	return array('serialNumber' => null, 'name' => null, 'price' => null, 'quantity' => null, 'manufacturer' => null, add => "add");
} 


?>